home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
INFO
/
PCCDEMO.ZIP
/
COMP1.EXE
/
BATCH.PRS
< prev
next >
Wrap
Text File
|
1993-12-20
|
12KB
|
199 lines
üÇôéç àêïäÆ ╬╠╬╠╧╧╬╠╬╠╬╠╬╠╬╠╡
│ In this edition, we will give
│ you a comprehensive introduction
┌─┐ ┌─┐ ┌─┐ │ to BATCH FILES, and how to
│ └──┐┌────┐│ └┐ ┌────┐│ └──┐ │ harness their power. In future
│ ┌┐ │├─── ││ ┌┘ │ ┌──┘│ ┌┐ │ │ issues, we will show you how to
│ └┘ ││ ── ││ └─┐│ └──┐│ ││ │ │ give your batch files more power
└────┘└────┘└───┘└────┘└─┘└─┘ │ and speed. As well as bringing
┌───┐ ┌─┐ │ you batch file tricks and hints,
│ ┌─┘┌─┐│ │┌────┐┌────┐ │ that will change the way you use
│ └┐ │ ││ ││ ── ││ ──┤ │ batch files.
│ ┌┘ │ ││ ││ ───┤├── │ │
│ │ └─┘└─┘└────┘└────┘ │
└─┘ σε± ∞≥-πε≥ │
│ One of the most powerful
ß√ απα∞ ΘΣφΩΦφ≥ │ features of Microsoft's MS-DOS is
│ undoubtedly its ability to
│ perform batch processing. This
∞α≥≤Σ±Φφµ ßα≤Γτ σΦδΣ≥ ∞απΣ Σα≥√ │ may seem strange, since
α ∞⌠≥≤ σε± αφ√ ∞≥-πε≥ ⌠≥Σ±. │ historically batch processing was
│ an unwieldy process which became
largely replaced by interactive │ For small tasks, writing a
processing once faster computers │ program in a powerful language
were developed. The days when │ like Borland's Turbo Pascal or
using a computer meant │ Microsoft C is overkill. The
laboriously writing programs on │ programs can become more
paper, using a card punch machine │ complicated than is really
to prepare punched cards, booking │ necessary and writing Pascal or C
a time to feed them into the card │ programs requires a suitable
reader of a computer, and collect │ compiler. A good solution is to
the results some time later are │ write a "program" using MS-DOS
long gone. Now it's mostly a │ commands in a batch file.
case of typing a command and │
getting instant results. So why │ Most operating systems have
use batch processing at all? │ facilities for batch processing;
│ DEC's VMS has the Digital Command
Well there are obviously │ Language and Unix has the Korn,
limits to what single DOS │ Bourne and C shells. The beauty
commands can do, even including │ of batch programs is that they
the use of pipes and filters (eg │ are easy to write and use, and
"dir | more" or "dir | sort"). │ quite adequate for small tasks,
making use of your PC easier and │ Almost any DOS application
more rewarding. Why spend hours │ that you use can be called from a
learning a new computer language │ batch file to make it work better
when you are already competent │ for you. Any command you can give
and comfortable with MS-DOS? │ to DOS using a keyboard can be
│ placed in a batch file and runs
The latest version of MS-DOS │ just as if you keyed it in.
is version 6, although most of │
the new features of version 6 │ Let's consider a simple batch
don't affect batch file │ file. It is called CHECK.BAT.
processing much, a CHOICE command │ Note that all MS-DOS batch files
has been added. This means, that │ have the extension .BAT to
now you can create real │ distinguish them from other
interactive batch files, with │ files. The contents of CHECK.BAT
menus, questions, etc.. │ (viewable by "type check.bat")
You could use programs like │ are:
the antivirus checker in a batch │
file; for example, to check for │ @echo off
viruses every time you turn on │ cls
your computer. │ echo This program checks multiple
disks in the A drive │ then run. Files with .COM or
:here │ .EXE are always run before .BAT
echo Insert next disk and │ files, thus if CHECK.COM or
pause │ CHECK.EXE exists you must type
chkdsk a: /f │ "check.bat" to run the batch
goto here │ file.
│
This file could be created by │ The batch file will keep
typing "copy con check.bat" and │ checking disks in drive A,
then the contents, followed by a │ pausing each time and asking the
Control-Z on a line by itself to │ user to press a key when ready.
end the file. │ To exit the batch file you
│ can use the Control-Break or
To run the batch file just │ Control-C key sequence. You will
type "check" by itself at the DOS │ then be prompted "Terminate
prompt, while in the same │ batch job Y/N?" where you
directory as the check.bat │ must respond Y if you want to
file. Unless a file exists │ quit, or N to continue running
called "check.com" or │ the batch program.
"check.exe", the file will │
Starting a command with an │ end containing the command "echo
@ character stops DOS from │ on".
echoing (showing) that command │
when the batch program runs. For │ The PAUSE command when used by
instance, in the above example, │ itself as in this program, simply
the user does not see "echo off" │ displays "Strike a key" but if
on the screen as that command │ followed by text then it displays
runs. And they do not see │ the text first. See the
"cls" etc either since the echo │ CHECK2.BAT program for an example
function has been turned off by │ of this.
the "echo off" command. All they │
see is what is specifically │ Like other programming
echoed to the screen.. the │ languages, batch files allow
message "Insert next disk" and │ control structures so that
also the message from the │ different instructions may be
PAUSE and CHKDSK commands. │ executed depending on the outcome
Once the batch file │ of events. In the above example
terminates, DOS goes back to │ the most primitive command
echoing commands by itself; you │ "GOTO" was used, and caused
do not need to have a line at the │ program execution to pass to the
specified label, which was │ echo This program checks multiple
specified by typing the label's │ disks in the %1 drive
name on a line by itself │ :here
preceded by a colon. │ pause Insert next disk and
│ chkdsk %1: /f
To determine whether or not │ if errorlevel 0 goto here
to change program execution, the │ echo Bye!
IF statement is used. Here's a │
better version of the CHECK.BAT │ The REM statement allows
progam, using the IF statement. │ remarks; lines starting with REM
│ are not executed but just serve
CHECK2.BAT: │ to remind the user what the
│ program is for, when it was
rem CHECK2.BAT program written in │ written, or any other pertinent
July 1993 │ information.
rem Usage: check2 <Drive> │
rem Checks disks in the drive │ As long as the error level is
specified │ zero, the program loops back to
@echo off │ "here". Otherwise it continues
cls │ running and thus comes to the
next command, the Bye message and │ The IF statement can also be
finishes. DOS returns an error │ used with the NOT parameter, eg
level of 0 from the previous │ if not errorlevel 3 goto start
command (CHKDSK here) if │ Which would send the program back
everything is okay and there were │ to the start for any exit code
no errors. An examples of an │ except for a value of 3.
error is the case of there being │
no disk in the drive! │ Yet another use of the IF
│ statement is to check if a file
The %1 indicates the first │ exists or not:
parameter; ie the first word │ if not exist %1.bak echo Warning
typed after the word "check2" on │ no backup!
the command line. In this case │
it is the drive identifier, eg A: │ Another useful control structure
or B: Up to nine parameters can │ is the FOR statement.
be specified by using %1 through │ AUTOEXEC.BAT:
to %9. The %0 parameter │ (this batch file is automatically
automatically contains the name │ run whenever the computer is
of the batch file. │ powered on or booted)
│
@echo off │ In the FOR statement,
break off │ parameters are preceded by %% and
cls │ can be any character except for
echo Checking all EXE files in │ characters 0 to 9. In this case
the root directory for viruses │ the parameter was called %%x and
for %%x in (\*.exe) do call │ was used so that for each .EXE
vcheck %%x │ file in the root directory, the
│ virus checking batch file was run
│ by using the CALL command.
VCHECK.BAT: │
@echo off │ The reason that the errorlevel
scan /nomem /nopause %1 > nul │ statements are as they are, is
if errorlevel 2 echo Program │ that the if errorlevel statement
Error (Out of memory?) │ also accepts greater values as
if errorlevel 2 goto end │ well as equal values. So if no
if errorlevel 1 pause VIRUS │ provision was made for exit code
FOUND! Reboot from clean system │ 2 (which occurs when the program
disk now │ is out of memory or similar), the
:end │ "if errorlevel 1" statement would
│ be executed and the user would
think they had a virus. │ Note that the examples given
│ are not necessarily the best
The VCHECK.BAT file then │ solutions, they were written
scanned the file given as a │ solely to illustrate various
parameter to it and if a virus │ facets of batch programming.
was found (indicated by a non │
zero errorlevel) then a message │ This concludes the tutorial, I
was displayed asking the user to │ hope that it has been useful ñ
reboot their computer from a │
"clean" disk. This is important │
in case the virus is memory │
resident, in which case accurate │
scanning may not be possible and │
more files may become infected. │
Not all virus checking programs │
will return error levels like │
this, these exit codes are │
specific to McAfee's excellent │
SCAN package. │
│